import { Body, Controller, Get, Post, Res, HttpStatus, UploadedFile, UseInterceptors, HttpException, Query, Request } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Response } from 'express';
import { InternetCelebrityGroup } from './internetCelebrityGroup.entity';
import { InternetCelebrityGroupService } from './internetCelebrityGroup.service';

import { User } from '../user/user.entity';
import { UserService } from '../user/user.service';
import { UserFuncController } from '../user/userFunc.controller';

import {
  apibefore
} from '../../router'
import utils from 'src/common/utils';
import { EnumInternetCelebrityCreateSourceEnum, EnumInternetCelebrityGroupTypeEnum } from './internetCelebrityGroup.enum';
import { ValidUniqueLoginInterceptor } from 'src/interceptor/validUniqueLogin';
@Controller(apibefore('/internetCelebrityGroupBackground'))
export class InternetCelebrityGroupBackgroundController extends UserFuncController {
  constructor(private readonly service: InternetCelebrityGroupService,
    private readonly userService: UserService
  ) {
    super(userService)
  }

  
  @Post('/delete_ic_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async Delete_IC_Auth(@Body() params, @Request() req){
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return this.service.deleteIc(params.ic_group)
  }

  @Post('/delete_ic')
  async Delete_IC(@Body() params, @Request() req){
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return this.service.deleteIc(params.ic_group)
  }

  
  @Post('/create_ic_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async Create_IC_Auth(@Body() params, @Request() req){
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let err = await this.service.createIc(params.ic_group, params.video_id)
    if(err){
      return new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return true
  }


  // @Post('/create_ic')
  // async Create_IC(@Body() params, @Request() req){
  //   //查询用户的wx_user
  //   let findUser = await this.findWxUser(req.headers.token)
  //   if(findUser.error){
  //     return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
  //   }
  //   return this.service.createIc(params.ic_group)
  // }

  
  /**
   * 创建网红
   * @param data
   * @param req
   * @returns
   */
  @Post('/create_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async create_auth(@Body() data: InternetCelebrityGroup, @Request() req) {
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let addData = new InternetCelebrityGroup()
    addData.name = data.name
    addData.wx_user = findUser.wx_user
    addData.create_time = new Date()
    addData.type = EnumInternetCelebrityGroupTypeEnum.common
    addData.create_source = EnumInternetCelebrityCreateSourceEnum.videolib
    try{
      let isExists = await this.service.findIsExists(findUser.wx_user, data.name)
      if(isExists){
        return new HttpException('网红已存在', HttpStatus.INTERNAL_SERVER_ERROR)
      }
      return await this.service.create(addData)
    }catch(err){
      let error = err.toString()
      if(error.indexOf('Duplicate entry') > -1 && error.indexOf(`key 'name'`) > -1){
        error = '网红已存在'
      }
      return new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
  }

  /**
   * 创建网红
   * @param data
   * @param req
   * @returns
   */
  @Post('/create')
  async create(@Body() data: InternetCelebrityGroup, @Request() req) {
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let addData = new InternetCelebrityGroup()
    addData.name = data.name
    addData.wx_user = findUser.wx_user
    addData.create_time = new Date()
    addData.type = EnumInternetCelebrityGroupTypeEnum.create
    try{
      let isExists = await this.service.findIsExists(findUser.wx_user, data.name)
      if(isExists){
        return new HttpException('网红已存在', HttpStatus.INTERNAL_SERVER_ERROR)
      }
      return await this.service.create(addData)
    }catch(err){
      let error = err.toString()
      if(error.indexOf('Duplicate entry') > -1 && error.indexOf(`key 'name'`) > -1){
        error = '网红已存在'
      }
      return new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
  }

  
  /**
   * 修改网红信息
   * @param data
   * @param req
   * @returns
   */
  @Post('/update_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async update_auth(@Body() data: InternetCelebrityGroup, @Request() req) {
   //查询用户的wx_user
   let findUser = await this.findWxUser(req.headers.token)
   if(findUser.error){
     return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
   }
   try{
     let isExists = await this.service.findIsExists(findUser.wx_user, data.name)
     if(isExists){
       return new HttpException('网红已存在', HttpStatus.INTERNAL_SERVER_ERROR)
     }
     return await this.service.update(data.id,data.name)
   }catch(err){
     let error = err.toString()
     if(error.indexOf('Duplicate entry') > -1 && error.indexOf(`key 'name'`) > -1){
       error = '网红已存在'
     }
     return new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR)
   }
  }

  /**
   * 修改网红信息
   * @param data
   * @param req
   * @returns
   */
   @Post('/update')
   async update(@Body() data: InternetCelebrityGroup, @Request() req) {
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    try{
      let isExists = await this.service.findIsExists(findUser.wx_user, data.name)
      if(isExists){
        return new HttpException('网红已存在', HttpStatus.INTERNAL_SERVER_ERROR)
      }
      return await this.service.update(data.id,data.name)
    }catch(err){
      let error = err.toString()
      if(error.indexOf('Duplicate entry') > -1 && error.indexOf(`key 'name'`) > -1){
        error = '网红已存在'
      }
      return new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
   }

   
  /**
   * 查询所有网红
   * @param params 
   * @param req 
   * @returns 
   */
  @Get('/findAll_ByUser_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async findAll_ByUser_auth(@Query() params: {pageSize:number, pageIndex: number, keyword: string, ic_group_id?: number}, @Request() req) {
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    const { skip, take } = utils.getLimit(params.pageSize, params.pageIndex)
    return await this.service.findAll_ByUser(skip, take, findUser.wx_user, params.keyword || '', params.ic_group_id)
  }

  /**
   * 查询所有网红
   * @param params 
   * @param req 
   * @returns 
   */
  @Get('/findAll_ByUser')
  async findAll_ByUser(@Query() params: {pageSize:number, pageIndex: number, keyword: string, ic_group_id?: number}, @Request() req) {
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    const { skip, take } = utils.getLimit(params.pageSize, params.pageIndex)
    return await this.service.findAll_ByUser(skip, take, findUser.wx_user, params.keyword || '', params.ic_group_id)
  }

  
  /**
   * 查询所有网红
   * @param params 
   * @param req 
   * @returns 
   */
  @Get('/findAll_ByUser_lib')
  async findAll_ByUser_lib(@Query() params: {pageSize:number, pageIndex: number, keyword: string, ic_group_id?: number}, @Request() req) {
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    const { skip, take } = utils.getLimit(params.pageSize, params.pageIndex)
    return await this.service.findAll_ByUser_lib(skip, take, params.keyword || '')
  }

  
  /**
   * 查询所有真实网红
   * @param params 
   * @param req 
   * @returns 
   */
  @Get('/findAll_ByUser_Ic_Info')
  async findAll_ByUser_Ic_Info(@Query() params: {pageSize:number, pageIndex: number, keyword: string, ic_group_id?: number}, @Request() req) {
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    const { skip, take } = utils.getLimit(params.pageSize, params.pageIndex)
    return await this.service.findAll_ByUser_Ic_Info(skip, take, params.keyword || '')
  }

  
  /**
   * 查询所有真实网红
   * @param params 
   * @param req 
   * @returns 
   */
  @Get('/findAll_ByUser_Ic_Info_NoPage')
  async findAll_ByUser_Ic_Info_NoPage(@Query() params: {keyword?: string, id_card?: string}, @Request() req) {
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return await this.service.findAll_ByUser_Ic_Info_NoPage(params.keyword || '', params.id_card || '')
  }

  
  

  
  /**
   * 查询所有网红
   * @param params 
   * @param req 
   * @returns 
   */
  @Get('/findAll_ByUser_NoPage_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async findAll_ByUser_NoPage_auth(@Query() params,@Request() req) {
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
     return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return await this.service.findAll_ByUser_NoPage(findUser.wx_user,params.keyword || '', params.byAccount)
  }


  /**
   * 查询所有网红
   * @param params 
   * @param req 
   * @returns 
   */
   @Get('/findAll_ByUser_NoPage')
   async findAll_ByUser_NoPage(@Query() params,@Request() req) {
     //查询用户的wx_user
     let findUser = await this.findWxUser(req.headers.token)
     if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
     }
     return await this.service.findAll_ByUser_NoPage(findUser.wx_user,params.keyword || '')
   }

   @Get('/findAll_NoPage')
   async findAll_NoPage(){
    return await this.service.findAll_NoPage()
   }

   @Post('delete_auth')
   @UseInterceptors(ValidUniqueLoginInterceptor)
   async delete_auth(@Body() data: InternetCelebrityGroup){
    let result:any = await this.service.remove(data.id)
    if(result){
      return new HttpException(result, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return true
  }

   @Post('delete')
   async delete(@Body() data: InternetCelebrityGroup){
    let result:any = await this.service.remove(data.id)
    if(result){
      return new HttpException(result, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return true
  }
}
